// define a SCORM object to interface with LMS
var oScorm = pipwerks.SCORM;  

// This function starts the course.  This function should be called first to ensure that
// all session variables are created
function loadOLM()
{
	// check to see if cookies is enabled, otherwise, this won't work
	if( typeof sessionStorage == "undefined" || sessionStorage.disabled )
	{
		alert( "You must enable cookies for this course to work properly." );
	}
	else
	{
		// if it is enabled, then check to see if this course was started
		// if it was not able to retrieve, the key "course_started", then
		// then this is the first time we are running the course
        if( !sessionStorage.getItem( "course_started" ) )
        {
        	// clear the session storage
            sessionStorage.clear();
            // set the "key" by giving it a value.  all values are strings!
            // course_started indicated that this OLM has started
    		sessionStorage.setItem( "course_started", "1" );
    			
			// set the values you want to persist throughout the website here
        
        
        }
	}
	
	// initializeSCORM after the OLM environment has been set up
	initializeSCORM();
}

// This function initializes the course by connecting the OLM to the LMS 
// The SCORM connection is done once at the beginning of the course
// Another function, reportScores(), will connect again when the 
// user is done with the online learning module
function initializeSCORM()
{
	var lmsConnected = oScorm.init();
	
	// we only want to run initializeSCORM once, so use course_started
	// to keep track of how many times we have initialized 
	var getStarted = sessionStorage.getItem( "course_started" );
	
	// only do the following if we are connected to the LMS and
	// if course_started has a value of 1
	if( lmsConnected && getStarted == "1" )
	{
        // always set the status to incomplete
		oScorm.set( "cmi.success_status", "unknown" );
		oScorm.set( "cmi.lesson_status", "incomplete" );
		oScorm.set( "cmi.completion_status", "incomplete" );
		
		// change course_started to another value so that this initializeSCORM
		// does not run the initialization code again; otherwise, this OLM
		// will be incomplete if the user goes back to the first page even after
		// passing the exam 
		sessionStorage.setItem( "course_started", "2" );
    		
		// retrieve the LMS values here and integrate them into your course
		
	}
}

//This function reports the score from the assessment to the LMS
//This should only be called when the user submits the answers to the quiz
function reportScores( percentage )
{	
	
	if( percentage > .6 )
	
	{
		oScorm.set( "cmi.success_status", "passed" );
		oScorm.set( "cmi.completion_status", "completed" );
		oScorm.set( "cmi.lesson_status", "passed" );
		//document.getElementById( "certificate-link" ).style.display = "inline";
	}
	else
	{
		oScorm.set( "cmi.success_status", "failed" );
		oScorm.set( "cmi.completion_status", "completed" );
		oScorm.set( "cmi.lesson_status", "failed" );
	}

	// these are some test values
	oScorm.set("cmi.score.max", "100" );
	oScorm.set("cmi.score.min", "0" );
	oScorm.set("cmi.score.raw", percentage * 100 );
	oScorm.set("cmi.score.scaled", percentage );
	oScorm.save();
}


// This function is called when the window is closed.  It saves and quits the course.
function finishOLM()
{
	oScorm.save();
	oScorm.quit();
}

//Always include this file if you want values to persist
//throughout the website
function startCourse()
{
	// check to see if cookies is enabled, otherwise, this won't work
	if( typeof sessionStorage == "undefined" || sessionStorage.disabled )
	{
		alert( "You must enable cookies for this course to work properly." );
	}
	else
	{
		// if it is enabled, checked to see if this course was started
		// if it was not able to retrieve, the key "course_started", then
		// then this is the first time we are running the course
     if( !sessionStorage.getItem( "course_started" ) )

     {
     		// clear the session storage
         sessionStorage.clear();
         // set the "key" by giving it a value.  all values are strings!
 			sessionStorage.setItem( "course_started", "1" );
 			
 			sessionStorage.setItem( "moon1", "unvisited" );
 			sessionStorage.setItem( "moon2", "unvisited" );
 			sessionStorage.setItem( "moon3", "unvisited" );
 			sessionStorage.setItem( "moon4", "unvisited" );	
 			sessionStorage.setItem( "sun1", "unvisited" );
 			sessionStorage.setItem( "sun2", "unvisited" );
 			sessionStorage.setItem( "sun3", "unvisited" );
 			sessionStorage.setItem( "sun4", "unvisited" );	
 			sessionStorage.setItem( "earth1", "unvisited" );
 			sessionStorage.setItem( "earth2", "unvisited" );	
 			sessionStorage.setItem( "earth3", "unvisited" );	
 			sessionStorage.setItem( "earth4", "unvisited" );	

     }
	}
}

//These functions to identify visited pages for the moon

function visitMoonpage1()
{
	sessionStorage.setItem( "moon1", "visited" );
	showTest();
}

function visitMoonpage2()
{
	sessionStorage.setItem( "moon2", "visited" );
	showTest();
}

function visitMoonpage3()
{
	sessionStorage.setItem( "moon3", "visited" );
	showTest();
}

function visitMoonpage4()
{
	sessionStorage.setItem( "moon4", "visited" );
	showTest();
}
function visitSunpage1()
{
	sessionStorage.setItem( "sun1", "visited" );
	showTest();
}

function visitSunpage2()
{
	sessionStorage.setItem( "sun2", "visited" );
	showTest();
}

function visitSunpage3()
{
	sessionStorage.setItem( "sun3", "visited" );
	showTest();
}

function visitSunpage4()
{
	sessionStorage.setItem( "sun4", "visited" );
	showTest();
}

function visitEarthpage1()
{
	sessionStorage.setItem( "earth1", "visited" );
	showTest();
}

function visitEarthpage2()
{
	sessionStorage.setItem( "earth2", "visited" );
	showTest();
}

function visitEarthpage3()
{
	sessionStorage.setItem( "earth3", "visited" );
	showTest();
}

function visitEarthpage4()
{
	sessionStorage.setItem( "earth4", "visited" );
	showTest();
}


//This function specifically to show the link of the quiz
function showTest()
{
	// retrieve all the visited values
	var moon1visited = sessionStorage.getItem( "moon1" );
	var moon2visited = sessionStorage.getItem( "moon2" );
	var moon3visited = sessionStorage.getItem( "moon3" );
	var moon4visited = sessionStorage.getItem( "moon4" );
	var sun1visited = sessionStorage.getItem( "sun1" );
	var sun2visited = sessionStorage.getItem( "sun2" );
	var sun3visited = sessionStorage.getItem( "sun3" );
	var sun4visited = sessionStorage.getItem( "sun4" );
	var earth1visited = sessionStorage.getItem( "earth1" );
	var earth2visited = sessionStorage.getItem( "earth2" );
	var earth3visited = sessionStorage.getItem( "earth3" );
	var earth4visited = sessionStorage.getItem( "earth4" );
	
	//conditions of visiting all pages
	if(moon1visited == "visited" && moon2visited == "visited" && moon3visited == "visited" && moon4visited == "visited" &&
		sun1visited == "visited" && sun2visited == "visited" && sun3visited == "visited" && sun4visited == "visited" &&
		earth1visited == "visited" && earth2visited == "visited" && earth3visited == "visited" && earth4visited == "visited")
		
	// This will bring the quiz button to show up when the user visit all the pages
	{
		document.getElementById( "test-Link" ).style.display = "inline";
	}
}




